home *** CD-ROM | disk | FTP | other *** search
- /* ****************************************************************
-
- ScriptAddILoc.c
- Written in Metrowerks C by Robert Thorne (rmt).
-
- Copyright © 1994-1995 Aladdin Systems, Inc.
- All Rights Reserved.
-
-
- Quick ILoc for Scripting Additions. Will create the folder if it is missing
-
- This file demostrates how to write the ILoc code resource that
- you build into an InstallerMaker 3.0 locations module.
-
- Last Revised: 8/3/95, rmt.
-
- ********************************************************************/
-
-
- #include "A4Globals.h" /* Quick trick to let us compile and test easier */
-
- #if !GLOBALS_ALWAYS_AVAILABLE
- #define ILOC_MAIN main
- #endif
-
- #include <Folders.h>
- #include "IMExtensionsFor3_0.h"
-
-
- // Prototype for the routine that does the real work
- static short DoILoc ( IMEnvironsRec *environs,
- short *destVol,
- long *destDir,
- short *killDestinationPrompt,
- unsigned long *refCon ) ;
-
-
- // Prototypes for a few utilities.
-
- static unsigned char *pStrcpy(unsigned char *dest, unsigned char *src) ;
- static Boolean FileOrDirExists ( short vRefNum, long dirID, Str255 name,
- OSType *typePtr, Boolean *isFolder ) ;
-
- //**** Implementation ******
-
-
- // Here's a shell for writing an ILoc 256:
-
- pascal short ILOC_MAIN ( IMEnvironsRec *environs,
- short *destVol,
- long *destDir,
- short *killDestinationPrompt,
- unsigned long *refCon )
- {
- long oldA4 ;
- short rslt ;
-
- // If we're running as a resource, we establish our A4 world here
- MAIN_SETUP_GLOBALS(oldA4) ;
-
- // Call our routine
- rslt = DoILoc ( environs, destVol, destDir, killDestinationPrompt, refCon ) ;
-
- MAIN_RESTORE_GLOBALS(oldA4) ; // Note we don't return until *after* this macro!!
-
- return rslt ;
-
- }
-
-
- static short DoILoc ( IMEnvironsRec *environs,
- short *destVol,
- long *destDir,
- short *killDestinationPrompt,
- unsigned long *refCon )
- {
- FSSpec spec ;
- OSType itsType ;
- long initDir ;
- long gestaltResult ;
- short initVol ;
- Boolean isFldr ;
- OSErr err ;
-
- // Worse case values for System 6
- *destVol = -1 ;
- *destDir = 2 ;
-
- // Make sure we're running under System 7.x. I'll test for the FSSpec calls.
- err = Gestalt (gestaltFSAttr, &gestaltResult ) ;
-
-
- if ( err || !(gestaltResult & (1 << gestaltHasFSSpecCalls)))
- return kILoc_SkipThisItem ;
-
-
- // First, let's get a reasonable default
- err = FindFolder ( environs->userSysVol, kTrashFolderType,
- kDontCreateFolder, destVol, destDir ) ;
-
- // Get the extensions folder for the target
- err = FindFolder ( environs->userSysVol, kExtensionFolderType,
- kDontCreateFolder, &initVol, &initDir ) ;
-
- if ( err )
- return kILoc_SkipThisItem ;
-
- err = FSMakeFSSpec ( initVol, initDir, "\p:Scripting Additions", &spec ) ;
-
- if ( err == fnfErr )
- {
- // Create it
- err = FSpDirCreate ( &spec, smSystemScript, &initDir ) ;
-
- if (!err )
- *destDir = initDir ;
- }
- else if (!err)
- {
- err = FileOrDirExists ( spec.vRefNum, spec.parID, spec.name, &itsType, &isFldr ) ;
-
- if ( isFldr ) // we're ok
- {
- //Quick trick to get the dirID
- err = FSMakeFSSpec ( spec.vRefNum, spec.parID,
- "\p:Scripting Additions:Bogus", &spec ) ;
-
- if (err == fnfErr)
- err = noErr ;
-
- initDir = spec.parID ;
- }
- else
- err = -1 ;
- }
-
-
- // If no error, we have something good to return
- if ( !err )
- {
- *destVol = initVol ;
- *destDir = initDir ;
-
- return kILoc_UseParameters ;
- }
- else
- return kILoc_SkipThisItem ;
-
- }
-
-
- // Utility routines...
-
- static Boolean FileOrDirExists ( short vRefNum, long dirID, Str255 name,
- OSType *typePtr, Boolean *isFolder )
- {
- // I do this by calling PBGetCatInfo, and checking ioFlAttrib
-
- CInfoPBRec pb ;
- OSErr err ;
- Str255 copyOfString ; // We don't want this walked on
- StringPtr nameParam = nil ;
-
- if ( name )
- nameParam = pStrcpy ( copyOfString, name ) ;
-
- // We need to set up a parameter block for a call to PBGetCatInfo. This
- // is documented in IM IV-155
-
- pb.hFileInfo.ioCompletion = nil ; // We intend to call the function synchronously
- pb.hFileInfo.ioNamePtr = nameParam ;
- pb.hFileInfo.ioVRefNum = vRefNum ;
- pb.hFileInfo.ioDirID = dirID ;
- pb.hFileInfo.ioFDirIndex = 0 ; // 0 indicates we are interested in the file or
- // directory named in ioNamePtr
-
- err = PBGetCatInfoSync ( &pb ) ;
-
- if ( err == noErr )
- {
- // First of all, we exist. First, see if we're a directory
-
- *isFolder = pb.hFileInfo.ioFlAttrib & ioDirMask ? true : false ;
-
- if ( !*isFolder )
- *typePtr = pb.hFileInfo.ioFlFndrInfo.fdType ;
-
- }
-
- return err == noErr ? true : false ;
- }
-
-
- static unsigned char *pStrcpy(unsigned char *dest, unsigned char *src)
- {
- BlockMoveData(src, dest, (long) *src + 1);
- return (dest);
- }
-